package com.ljm.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljm.common.page.PageDto;
import com.ljm.notice.entity.LjmNotice;
import com.ljm.notice.mapper.LjmNoticeMapper;
import com.ljm.notice.service.ILjmNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljm.notice.api.LjmNoticeApi;
import com.ljm.notice.dto.LjmNoticeDto;
import com.ljm.security.util.UserUtil;
import com.ljm.service.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 新闻公告 服务实现类
 * </p>
 *
 * @author HuamingChen
 * @since 2020-10-29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LjmNoticeServiceImpl extends BaseServiceImpl<LjmNoticeMapper, LjmNotice>
        implements ILjmNoticeService, LjmNoticeApi {

    @Override
    public void saveData(LjmNoticeDto dto) {
        LjmNotice po = mapperFacade.map(dto, LjmNotice.class);
        if(dto.getId()==null) {
            po.setCreater(UserUtil.getLoginName());
            po.setCreateTime(LocalDateTime.now());
            po.setIsDelete(Boolean.FALSE);
        }else{
            po.setEditor(UserUtil.getLoginName());
            po.setEditTime(LocalDateTime.now());
        }
        this.saveOrUpdate(po);
    }

    @Override
    public void batchDel(List<Long> ids) {
        this.removeByIds(ids);
    }

    @Override
    public LjmNoticeDto getDataById(Long id) {
        LjmNotice po = this.getById(id);
        if(Optional.of(po).isPresent()){
            return mapperFacade.map(po,LjmNoticeDto.class);
        }else{
            return null;
        }
    }

    @Override
    public PageDto<LjmNoticeDto> getDataByPage(LjmNoticeDto ljmNoticeDto) {
        PageDto<LjmNoticeDto> pageDto=new PageDto<>(ljmNoticeDto.getPageNo(),ljmNoticeDto.getPageSize());
        IPage<LjmNotice> page=new Page<>(ljmNoticeDto.getPageNo(),ljmNoticeDto.getPageSize());
        LambdaQueryWrapper<LjmNotice> wrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(ljmNoticeDto.getTitle())){
            wrapper.like(LjmNotice::getTitle,ljmNoticeDto.getTitle());
        }
        if(StringUtils.isNotEmpty(ljmNoticeDto.getFdOrigin())){
            wrapper.like(LjmNotice::getFdOrigin,ljmNoticeDto.getFdOrigin());
        }
        if(ljmNoticeDto.getFdType()!=null){
            wrapper.eq(LjmNotice::getFdType,ljmNoticeDto.getFdType());
        }
        page=this.page(page,wrapper);
        if(page.getTotal()>0){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(mapperFacade.mapAsList(page.getRecords(),LjmNoticeDto.class));
        }
        return pageDto;
    }

    @Override
    public Map<Class, Class> classMap() {
        Map<Class,Class> map=new HashMap<>(1);
        map.put(LjmNotice.class,LjmNoticeDto.class);
        return map;
    }
}
